<?php
namespace app\api\model;
use think\Model;
use think\Db;

class AssetsLedger extends Model{
    protected $type = [
        'id'    =>  'integer',
        'type'    =>  'integer',
        'num' =>'integer',
        'assets_id'    =>  'integer',
        'salesman_id'    =>  'integer',
        'branch_id'    =>  'integer',   
        'is_review'    =>  'integer',      
    ];
    //关联收入表   
    public function assets(){
        return $this->hasOne('assets','id','assets_id')->with([
            'assetsType'=>'assetsType',
            'assetsModel'=>'assetsModel',
            'assetsUnit'=>'assetsUnit'           
        ]);
    }     
    public function operator(){
        return $this->hasOne('manager','id','operator_id')->field('id,cn_name');
    }
    public function salesman(){
        return $this->hasOne('manager','id','salesman_id')->field('id,cn_name');
    }
    public function branch(){
        return $this->hasOne('category','id','branch_id');
    }
    public function Lists($filter = null){
        $where = [
            'type' => $filter['type']
        ];       
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
            //指定id查询
            if(!empty($filter['id'])){
                $where['id'] = $filter['id'];
            }           
            if(!empty($filter['start_date']) && !empty($filter['end_date'])){                    
                $where['use_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = ['like',"%{$filter['sn']}%"];
            }
            
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
           
            if(!empty($filter['assets_id'])){
                $where['assets_id'] = $filter['assets_id'];
            }          
        
            if(!empty($filter['branch_id'])){   
                $where['branch_id'] = $filter['branch_id'];
            }
        
            if(!empty($filter['salesman_id'])){
                $where['salesman_id'] = $filter['salesman_id'];
            }           
        
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];               
            }
        }
        
        $datas = $this->where($where)->with([
            'assets'=>'assets',          
            "branch"=>'branch',
            'salesman'=>'salesman',
            'operator'=>'operator'
        ])
        ->page("$page,$pageCount")->order('id', 'desc')->select();      
        
        $dataCount =  $this->where($where)->count();       
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;      
    }

    public function allLists($filter = null){
        $where = [
            'type' => $filter['type']
        ];
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
           //指定id查询
           if(!empty($filter['id'])){
            $where['id'] = $filter['id'];
            }   
            if(!empty($filter['start_date']) && !empty($filter['end_date'])){        
            $where['use_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
            }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = ['like',"%{$filter['sn']}%"];
            }
           
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
        
            if(!empty($filter['assets_id'])){
                $where['assets_id'] = $filter['assets_id'];
            }          
        
            if(!empty($filter['branch_id'])){   
                $where['branch_id'] = $filter['branch_id'];
            }
        
            if(!empty($filter['salesman_id'])){
                $where['salesman_id'] = $filter['salesman_id'];
            }           
        
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];               
            }
        }
        
        $datas = $this->where($where)->with([
            'assets'=>'assets',   
            "branch"=>'branch',
            'salesman'=>'salesman',
            'operator'=>'operator'
        ])
        ->order('id', 'desc')->select();   
         
        return $datas;      
    }
    public function excel($filter = null){
        $where = [
            'type' => $filter['type']
        ];
        $page = 1;
        $pageCount = 10;
        $type = $filter['type'];
        
        if(!empty($filter)){
            //指定id查询
           if(!empty($filter['id'])){
            $where['id'] = $filter['id'];
            }           
            if(!empty($filter['start_date']) && !empty($filter['end_date'])){        
                $where['use_date'] = [['>= time',$filter['start_date']],['<= time',$filter['end_date']]];
                }
            //查询团号
            if(!empty($filter['sn'])){
                $where['sn'] = ['like',"%{$filter['sn']}%"];
            }
            //模糊查询           
            if(!empty($filter['keyword'])){
                $where['assets_name'] = ['like',"%{$filter['keyword']}%"];               
            }
            //页码            
            $page = !empty($filter['page']) ? $filter['page'] : $page;
            $pageCount = !empty($filter['page_count']) ? $filter['page_count'] : $pageCount;
        
            if(!empty($filter['assets_type'])){
                $where['assets_type'] = $filter['assets_type'];
            }          
        
            if(!empty($filter['assets_model'])){   
                $where['assets_model'] = $filter['assets_model'];
            }
        
            if(!empty($filter['assets_unit'])){
                $where['assets_unit'] = $filter['assets_unit'];
            }           
        
            if(!empty($filter['operator_id'])){
                $where['operator_id'] = $filter['operator_id'];              
            }
        }
        
        $datas = $this->where($where)->page("$page,$pageCount")->order('id', 'desc')->select();   
       
        return $datas;      
    }
}